-
Notifications
You must be signed in to change notification settings - Fork 30
feat: add --existing-policy flag in verify-policy command for example policies #1189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
30b51e3 to
ce3dfd5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a unit test to tests/policy_engine/test_policy.py. Also add an integration test.
src/macaron/__main__.py
Outdated
| with open(verify_policy_args.file, encoding="utf-8") as file: | ||
| policy_content = file.read() | ||
| elif verify_policy_args.policy: | ||
| policy_dir = os.path.join(macaron.MACARON_PATH, "resources/policies/datalog") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We avoid using / in strings and instead use the os.path.join method to construct the path.
| policy_dir = os.path.join(macaron.MACARON_PATH, "resources/policies/datalog") | |
| policy_dir = os.path.join(macaron.MACARON_PATH, "resources", "policies", "datalog") |
src/macaron/__main__.py
Outdated
| policy_content = file.read() | ||
| elif verify_policy_args.policy: | ||
| policy_dir = os.path.join(macaron.MACARON_PATH, "resources/policies/datalog") | ||
| available_policies = [policy[:-12] for policy in os.listdir(policy_dir) if policy.endswith(".dl.template")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to avoid hardcoding [:-12].
| available_policies = [policy[:-12] for policy in os.listdir(policy_dir) if policy.endswith(".dl.template")] | |
| policy_suffix = ".dl.template" | |
| available_policies = [ | |
| os.path.splitext(policy)[0].replace(policy_suffix, "") | |
| for policy in os.listdir(policy_dir) | |
| if policy.endswith(policy_suffix) | |
| ] |
src/macaron/__main__.py
Outdated
| policy_path = os.path.join(policy_dir, f"{verify_policy_args.policy}.dl.template") | ||
| with open(policy_path, encoding="utf-8") as file: | ||
| policy_content = file.read() | ||
| if verify_policy_args.package_url: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's check that the PURL using the packageurl library, which is already a dependency of Macaron:
try:
PackageURL.from_string(verify_policy_args.package_url)
except ValueError as error:
logger.error("The package url %s is not valid.", verify_policy_args.package_url)
return os.EX_USAGE
src/macaron/__main__.py
Outdated
| vp_parser.add_argument("-d", "--database", required=True, type=str, help="Path to the database.") | ||
| vp_parser.add_argument("-purl", "--package-url", help="PackageURL for policy template.") | ||
| vp_group.add_argument("-f", "--file", type=str, help="Path to the Datalog policy.") | ||
| vp_group.add_argument("-p", "--policy", help="Example policy to run.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| vp_group.add_argument("-p", "--policy", help="Example policy to run.") | |
| vp_group.add_argument("-e", "--existing-policy", help="Name of the existing policy to run.") |
ae722e9 to
a2203de
Compare
Signed-off-by: Demolus13 <[email protected]>
Signed-off-by: Demolus13 <[email protected]>
Signed-off-by: Demolus13 <[email protected]>
Signed-off-by: Demolus13 <[email protected]>
…olicy command Signed-off-by: Demolus13 <[email protected]>
Signed-off-by: Demolus13 <[email protected]>
40dbe23 to
ff9fff7
Compare
|
|
||
| apply_policy_to("check-dependencies", component_id) :- | ||
| is_component(component_id, purl), | ||
| match("<PACKAGE_PURL>@.*", purl). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the user provides a PURL that contains version as well? Then the policy won't match the PURL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does removing @.* resolve the issue?
from
match("<PACKAGE_PURL>@.*", purl).
to
match("<PACKAGE_PURL>", purl).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but that would mean we can't use wildcards, which are useful for creating more generic policies that apply to all versions. Alternatively, we could allow the wildcard within the PURL argument itself 🤔
Summary
This Pull Request introduces a new
--existing-policyflag to theverify-policycommand, allowing users to run example policies by name without specifying a file path. It also adds support for policy templates.The new
--existing-policy(-e) flag executes a predefined policy template by name. If the template exists, it populates the<PACKAGE_PURL>placeholder with the value from the--package-url(-purl) argument and runs the policy. If the template name is not found, it lists all available templates.Example policies
Description of changes
--existing-policy(-e) argument to theverify-policyCLI command, enabling users to select and run example policies from the built-in resources.resources/policies/datalogdirectory.--package-url(-purl) argument to substitute the<PACKAGE_PURL>placeholder in policy templates.--existing-policy(-e) flag intests/policy_engine/test_existing_policy.pyRelated issues
N/A
Checklist
verifiedlabel should appear next to all of your commits on GitHub.